﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using LKVMLMADMIN.Common;
using System.Configuration;
using PinService.Lib;
using PagedList;

namespace LKVMLMADMIN.Controllers.Html
{
    public class PaymentController : Controller
    {
        //
        // GET: /Payment/
        public ActionResult Index()
        {
            var stringhelper = new StringHelper();
            var transaction = new LKVService.Transaction();
            var UserDB = new LKVService.User();            
            var transactionid = 0;
            //ViewBag.Result = "Payment Unsuccessful";
            ViewBag.Result = "Add fund successful, please close app and open again";
            //BitConverter.ToString()
            if(Request.Form!=null)  
            {
                var v2_hash = Request.Form["V2_HASH"];
                string ALTERNATE_PHRASE = ConfigurationManager.AppSettings["ALTERNATE_PHRASE"].ToString();                
                string ALTERNATE_PHRASE_HASH = stringhelper.MD5Hash(ALTERNATE_PHRASE).ToUpper();
                string str =
                          Request.Form["PAYMENT_ID"] + ':' + Request.Form["PAYEE_ACCOUNT"] + ':' +
                          Request.Form["PAYMENT_AMOUNT"] + ':' + Request.Form["PAYMENT_UNITS"] + ':' +
                          Request.Form["PAYMENT_BATCH_NUM"] + ':' +
                          Request.Form["PAYER_ACCOUNT"] + ':' + ALTERNATE_PHRASE_HASH + ':' +
                          Request.Form["TIMESTAMPGMT"];
                var hash = stringhelper.MD5Hash(str).ToUpper();
                if (hash == v2_hash)
                {
                    //var requestTransaction = new LKVService.formPerfectTransaction();
                    var requestTransaction = transaction.getPerfectMoneyTransaction(int.Parse(Request.Form["PAYMENT_ID"]));
                    if (requestTransaction.V2_Hash == null)
                    {
                        //thanh cong
                        //chuyen tien vao vi                                        
                        var perfectTransaction = transaction.getPerfectMoneyTransaction(requestTransaction.Id);
                       
                        //luu lich su giao dich
                        transaction.chargeFormWallet(perfectTransaction.UserId, 1, perfectTransaction.Payment_Amount, "Add fund with batch no " + Request.Form["PAYMENT_BATCH_NUM"], Request.Form["PAYMENT_BATCH_NUM"], 1);                        
                        //cap nhat lai transaction Perfect Money
                        requestTransaction.Payment_Batch_Num = Request.Form["PAYMENT_BATCH_NUM"];
                        requestTransaction.Payer_Account = Request.Form["PAYER_ACCOUNT"];
                        requestTransaction.TimeStampgmt = Request.Form["TIMESTAMPGMT"];
                        requestTransaction.V2_Hash = v2_hash;
                        requestTransaction.Payment_Amount = double.Parse(Request.Form["PAYMENT_AMOUNT"]);
                        //get transaction id
                        transactionid = transaction.UpdatePerfectMoneyTransaction(requestTransaction);
                        var AccountPerfectMoney = transaction.getAccountPerfectMoney(requestTransaction.UserId);
                        //luu lai tai khoan perfecmoney
                        if (AccountPerfectMoney==null)
                        {
                            UserDB.UpdatePerfectMoney(requestTransaction.UserId, "USD", requestTransaction.Payer_Account, requestTransaction.Payer_Account, requestTransaction.Payee_Name);                        
                        }
                        ViewBag.Result = "Add fund successful, please close app and open again";
                        transaction.AfterAddFund(requestTransaction.UserId, requestTransaction.Payment_Amount, requestTransaction.Order_Num, DateTime.Now, 1);
                    }
                }            
            }
            return Redirect("http://localhost/callback");
        }
        
        public ActionResult Status()
        {
            var a = Request.Form["k"];
            ViewBag.Result = "Perfect Cancel";
            return View();
        }
        public ActionResult Cancel()
        {
            var a = Request.Form["k"];
            ViewBag.Result = "Perfect Cancel";
            return View();
        }
        public ActionResult submitperfectmoney(int id=0)
        {
            if(id!=0){
                // ở đây em tra về from ở view va tu dong submit cai form đo giúp anh
                var transaction = new LKVService.Transaction();            
                var perfectTransaction = transaction.getPerfectMoneyTransaction(id);
                return View(perfectTransaction);
            }
            else
            {
                return RedirectToAction("Login", "Account");
            }           
        }
        public MaThePaymentService pinService = new MaThePaymentService();
        
    }
}
